/**
 * 获取host域名
 * @returns
 */
function hostUrl() {
    var storeId = 3578748;
    var pathname = location.pathname;
    if (pathname) {
        var urlArray = pathname.split('/');
        var regPos = /^\d+(\.\d+)?$/;
        if (regPos.test(urlArray[1])) {
            storeId = urlArray[1];
        }
    }
    return location.protocol+'//'+location.host+'/'+storeId+'/login/';
}

$(function(){
    function validMobile(tel){
        var reg = /^1[2|3|4|5|6|7|8|9][0-9]\d{8}$/;
        return reg.test(tel);
    }
    
    function validEmail(email){
        var reg = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/;    
        return reg.test(email);
    }
  
    function alertMessage(msg)
    {
        $('#my-alert .alert-content').text(msg);
        $('#my-alert').addClass('am-modal-active');
        setTimeout(function(){
            $('#my-alert').removeClass('am-modal-active');
        },2000);
    }
    
    //快捷登录
    var qlogin = {
        countdown: 60,
        setTime: function(obj) {
            if (qlogin.countdown == 0) {
                obj.removeClass("disabled");
                obj.text("获取验证码");
                qlogin.countdown = 60;
            } else {
                obj.addClass("disabled");
                obj.text("重新发送(" + qlogin.countdown + "s)");
                qlogin.countdown--;
                setTimeout(function() {
                    qlogin.setTime(obj);
                },1000);
            }
        },
        select: function() {
            var flag = $('input[name=flag]').val();
            if (flag == 1) {
                $('.quick-login').show();
                $('.normal-login').hide();
            }else{
                $('.quick-login').hide();
                $('.normal-login').show();
            }
        },
        verify: function(){
            $('.quick-login').on('click', '.send-verify', function(){
                if (!$(this).hasClass('disabled')) {
                    var obj = $(this);
                    var mobile_phone = $.trim($('.quick-login input[name=mobile_phone]').val());
                    if (!validMobile(mobile_phone)) {
                        alertMessage('请输入正确的手机号码');
                        return;
                    }
                    $.ajax({
                        type:'post',
                        async:false,
                        dataType:'json',
                        url:hostUrl()+'login/checkPhone',
                        data:{mobile_phone:mobile_phone},
                        beforeSend:function(){

                        },
                        success:function(json){
                            if (!json.status) {
                                alertMessage(json.msg);
                            } else {
                                qlogin.setTime(obj);
                            }
                        }
                    });
                }
            });
        },
        submit: function() {
            $('.quick-login').on('click', '.login-submit', function(event) {
                var obj = $(".quick-login .login-input-verify");
                var verify = $.trim($('.quick-login input[name=verify]').val());
                var captcha = $.trim($('.quick-login input[name=captcha]').val());
                if (!$(this).hasClass('btn-disabled')) {
                    var mobile_phone = $.trim($('.quick-login input[name=mobile_phone]').val());
                    if (!validMobile(mobile_phone)) {
                        alertMessage('请输入正确的手机号码');
                        return;
                    }
                    if (!verify) {
                        alertMessage('请输入手机验证码');
                        return;
                    }
                    $.ajax({
                        type : 'post',
                        async : false,
                        dataType : 'json',
                        url : hostUrl() + 'login/quickLogin',
                        data : $('.quick-login form.login-form').serialize(),
                        beforeSend : function() {
                            $('.login-submit span').text('正在登录...').attr('disabled', true);
                        },
                        success : function(d) {
                            if (d.status) {
                                window.location.href = d.msg;
                            } else {
                                alertMessage(d.msg);
                                if (d.data >= 3) {
                                    obj.css('display', 'block');
                                }
                            }
                            $('.login-submit span').animate({'top':'+=0'}, 200, function(){
                                $(this).text('登　录').removeAttr('disabled');
                            });
                        }
                    });
                }
            });
        },
        init: function(){
            qlogin.select();
            qlogin.verify();
            qlogin.submit();
        }    
    };
    
    //会员登录
    var nlogin = {
            captcha: function() {
                $('.quick-login').on('click', '.verify', function(event){
                    $.ajax({
                        type: 'post',
                        dataType : 'json',
                        url: hostUrl()+'login/checkCaptcha',
                        success: function(d) {
                            $('.verify').html(d.image);
                        },
                        error : function(xhq, status, error) {
                            console.log(status+'-'+error);
                        }
                    });
                    event.preventDefault();
                });
                $('.normal-login').on('click', '.verify', function(event){
                    $.ajax({
                        type: 'post',
                        dataType : 'json',
                        url: hostUrl()+'login/checkCaptcha',
                        success: function(d) {
                            $('.verify').html(d.image);
                        },
                    });
                    event.preventDefault();
                });
            },
            submit: function() {
                $('.normal-login').on('click', '.login-submit', function(event) {
                    var obj = $(".normal-login .login-input-verify");
                    var mobile_phone = $.trim($('.d-mobile').val());
                    var pw = $.trim($('input[name=pw]').val());
                    var captcha = $.trim($('.d-captcha').val());
                    var flag = $(this).data('flag');
                    if (!$(this).hasClass('btn-disabled')) {
                        if (!mobile_phone) {
                            alertMessage('请输入账户名');
                            return;
                        }
                        if (!pw) {
                            alertMessage('请输入密码');
                            return;
                        }
                        if (pw.length < 6 || pw.length > 18) {
                            alertMessage('密码为6-18位数字和字母组合');
                            return;
                        }
                        $.ajax({
                            type : 'post',
                            async : false,
                            dataType : 'json',
                            url : hostUrl() + 'login/normalLogin',
                            data : $('.normal-login form.login-form').serialize(),
                            beforeSend : function() {
                                $('.login-submit span').text('正在登录...').attr('disabled', true);
                            },
                            success : function(d) {
                                if (d.status) {
                                    window.location.href = d.msg;
                                } else {
                                    if (d.data >= 3) {
                                        obj.css('display', 'block');
                                    }
                                    alertMessage(d.msg);
                                }
                                $('.login-submit span').animate({'top':'+=0'}, 200, function(){
                                    $(this).text('登　录').removeAttr('disabled');
                                });
                            }
                        });
                    }
                });
            },
            init: function(){
                nlogin.captcha();
                nlogin.submit();
            }    
        };

    //忘记密码
    var forget = {
            verify: function(){
                $('.d-modify-mobile').on('click', '.send-verify', function(){
                    if(!$(this).hasClass('disabled')){
                        var obj = $(this);
                        var mobile_phone = $.trim($('.d-modify .d-mobile').val());
                        if (!validMobile(mobile_phone)) {
                            alertMessage('手机号码格式有误');
                            return;
                        }
                        $.ajax({
                            type:'post',
                            async:false,
                            dataType:'json',
                            url:hostUrl()+'forget/checkPhone',
                            data:{mobile_phone:mobile_phone},
                            beforeSend:function(){
                                        
                            },
                            success:function(json){
                                if(!json.status){
                                    alertMessage(json.msg);
                                }else{
                                    qlogin.setTime(obj);
                                }
                            }
                        });
                    }
                });
                $('.d-modify-email').on('click', '.send-verify', function(){
                    if(!$(this).hasClass('disabled')){
                        var obj = $(this);
                        var user_email = $.trim($('.d-modify .d-email').val());
                        if (!validEmail(user_email)) {
                            alertMessage('邮箱格式有误');
                            return;
                        }
                        $.ajax({
                            type:'post',
                            async:false,
                            dataType:'json',
                            url:hostUrl()+'forget/checkEmail',
                            data:{user_email:user_email},
                            beforeSend:function(){
                                        
                            },
                            success:function(json){
                                if(!json.status){
                                    alertMessage(json.messages);
                                }else{
                                    qlogin.setTime(obj);
                                }
                            }
                        });
                    }
                });
            },
            confirm: function(){
                $('.d-forget').hide();
                $('.d-confirm').show();
                $('.d-confirm').on('click', '.forget-btn', function(){
                    if(!$(this).hasClass('disabled')){
                        var mobile_phone = $.trim($('input[name=mobile_phone]').val());
                        if (!mobile_phone) {
                            alertMessage('请输入账户名');
                            return;
                        }
                        $.ajax({
                            type:'post',
                            async:false,
                            dataType:'json',
                            url:hostUrl()+'forget/forgetpost',
                            data:$('.d-confirm .forget-form').serialize(),
                            beforeSend:function(){
                                $('.d-confirm .forget-btn span').text('正在下一步...').attr('disabled', true);    
                            },
                            success:function(d){
                                if(d.status){
                                    $('.d-forget').hide();
                                    $('.d-select').show();
                                    $('.d-mobile').val(d.data.mobile_phone);
                                    $('.d-email').val(d.data.user_email);
                                }else{
                                    alertMessage(d.msg);
                                }
                                $('.d-confirm .forget-btn span').text('下一步').removeAttr('disabled');    
                            }
                        });
                    }
                });
            },
            select: function(){
                $('.d-select').on('click', '.forget-btn', function(){
                    if(!$(this).hasClass('disabled')){
                        var account = $('input:radio:checked').val();
                        var type = $('.d-select .d-type').val();
                        $.ajax({
                            type:'post',
                            async:false,
                            dataType:'json',
                            url:hostUrl()+'forget/forgetpost',
                            data:$('.d-select .forget-form').serialize(),
                            beforeSend:function(){
                                $('.d-select .forget-btn span').text('正在下一步...').attr('disabled', true);    
                            },
                            success:function(d){
                                if(d.status){
                                    $('.d-forget').hide();
                                    if (d.data.mobile_phone) { //手机
                                        var mobile = d.data.mobile_phone;
                                        _mobile = mobile.substr(0, 3) + '****' + mobile.substr(-4, 4);
                                        $('.d-text').text('+86 ' + _mobile);
                                        $('.d-mobile').val(d.data.mobile_phone);
                                        $('.d-modify-mobile').show();
                                        $('.d-modify-mobile .send-verify').click();
                                    }else{
                                        var email = d.data.user_email.split('@');
                                        _email = email[0].substr(0, 1) + '****' + email[0].substr(-1, 1) + '@' + email[1];
                                        $('.d-text').text(_email);
                                        $('.d-email').val(d.data.user_email);
                                        $('.d-modify-email').show();
                                        $('.d-modify-email .send-verify').click();
                                    }
                                }else{
                                    alertMessage(d.msg);
                                }
                                $('.d-select .forget-btn span').text('下一步').removeAttr('disabled');    
                            }
                        });
                    }
                });
                $('.d-select').on('click', '.return-login', function(){
                    $('.d-forget').hide();
                    $('.d-confirm').show();
                });
            },
            modify: function() {
                $('.d-modify').on('click', '.forget-btn', function(){
                    var verify = $.trim($(this).prevAll().find("input[name=verify]").val());
                    var pw = $.trim($(this).prevAll().find("input[name=pw]").val());
                    if (!$(this).hasClass('btn-disabled')) {
                        if (!verify) {
                            alertMessage('请输入验证码');
                            return;
                        }
                        if (!pw) {
                            alertMessage('请输入密码');
                            return;
                        }
                        $.ajax({
                            type:'post',
                            async:false,
                            dataType:'json',
                            url:hostUrl()+'forget/forgetpost',
                            data:$(this).parents('.forget-form').serialize(),
                            beforeSend:function(){
                                $('.d-modify .forget-btn span').text('正在确定...').attr('disabled', true);    
                            },
                            success:function(d){
                                if(d.status){
                                    $('.d-forget').hide();
                                    $('.d-success').show();
                                }else{
                                    alertMessage(d.msg);
                                }
                                $('.d-modify .forget-btn span').text('确定').removeAttr('disabled');
                            }
                        });
                    }
                });
                $('.d-modify').on('click', '.return-login', function(){
                       $('.d-forget').hide();
                       $('.d-select').show();
                });
                $('.d-success').on('click', '.return-login', function(){
                       $('.d-forget').hide();
                       $('.d-select').show();
                });
            },
            init: function(){
                forget.verify();
                forget.confirm();
                forget.select();
                forget.modify();
            }    
        };
    
    
    //原先注册
    var register = {
            countdown:90,
            userNameChange:function(){
                $('#register-from').on('keyup', 'input[name=username]', function(){
                    register.enableSubmit();
                });
            },
            passwordChange:function(){
                $('#register-from').on('keyup', 'input[name=password]', function(){
                    register.enableSubmit();
                });
            },
            configPasswordChange:function(){
                $('#register-from').on('keyup', 'input[name=confirmPassword]', function(){
                    register.enableSubmit();
                });
            },
            captchaChange:function(){
                $('#register-from').on('keyup', 'input[name=verify]', function(){
                    register.enableSubmit();
                });
            },
            enableSubmit:function(){
                //var username = $.trim($('input[name=username]').val());
                //var password = $.trim($('input[name=password]').val());
                //var confirmPassword = $.trim($('input[name=confirmPassword]').val());
                //var captcha = $.trim($('input[name=verify]').val());
                //if(username.length > 0 && password.length > 0 && confirmPassword.length > 0 && captcha.length > 0 && validMobile(username)){
                //    $('#register-submit').removeClass('btn-disabled');
                //} else {
                //    $('#register-submit').addClass('btn-disabled');
                //}
            },
            setTime: function(obj) {
                if (register.countdown == 0) {
                    obj.removeClass("disabled");
                    obj.text("获取验证码");
                    register.countdown = 90;
                } else {
                    obj.addClass("disabled");
                    obj.text("重新发送(" + register.countdown + ")");
                    register.countdown--;
                    setTimeout(function() {
                        register.setTime(obj);
                    },1000);
                }
            },
            getCaptcha:function(){
                $('#register-from').on('click', '#getCaptcha', function(){
                    if(!$(this).hasClass('disabled')){
                        var username = $.trim($('input[name=username]').val());
                        if(validMobile(username)){
                            var obj = $(this);
                            $.ajax({
                                type:'post',
                                async:false,
                                dataType:'json',
                                url:hostUrl()+'reg/checkPhone',
                                data:{username:username},
                                beforeSend:function(){

                                },
                                success:function(json){
                                    if(!json.status){
                                        alertMessage(json.msg);
                                    }else{
                                        register.setTime(obj);
                                    }
                                }
                            });
                        } else {
                            alertMessage('手机格式错误');
                        }
                    }
                });
            },
            submit:function(){
                $('#register-from').on('click', '#register-submit', function(event){
                    if(!$(this).hasClass('btn-disabled')){
                        var username = $.trim($('input[name=username]').val());
                        var password = $.trim($('input[name=password]').val());
                        var confirmPassword = $.trim($('input[name=confirmPassword]').val());
                        if(!validMobile(username)){
                            alertMessage('手机格式错误');
                            return false;
                        }
                        if(password.length < 6){
                            alertMessage('请输入6-20位的密码');
                            return false;
                        }
                        if(password != confirmPassword){
                            alertMessage('2次输入的密码不一致');
                            return false;
                        }
                        $.ajax({
                            type:'post',
                            async:false,
                            dataType:'json',
                            url:hostUrl()+'reg/toRegister',
                            data:$('form').serialize(),
                            beforeSend:function(){

                            },
                            success:function(json){
                                if(json.status){
                                    window.location.href = json.msg;
                                }else{
                                    alertMessage(json.msg);
                                }
                            }
                        });
                    }
                });
            },
            init:function(){
                register.userNameChange();
                register.passwordChange();
                register.captchaChange();
                register.configPasswordChange();
                register.submit();
                register.getCaptcha();
            }
        };

        var voucher = {
            countdown:90,
            setTime: function(obj) {
                if (voucher.countdown == 0) {
                    obj.removeClass("disabled");
                    obj.text("获取验证码");
                    voucher.countdown = 90;
                } else {
                    obj.addClass("disabled");
                    obj.text("重新发送(" + voucher.countdown + ")");
                    voucher.countdown--;
                    setTimeout(function() {
                        voucher.setTime(obj);
                    },1000);
                }
            },
            verify: function(){
                $('form.login-form').on('click', '.send-verify', function(){
                    if(!$(this).hasClass('disabled')){
                        var phone = $.trim($('input[name=mobile_phone]').val());
                        var obj = $(this);
                        if(validMobile(mobile_phone)){
                            alertMessage('手机格式错误');
                            return;
                        }
                        $.ajax({
                            type:'post',
                            async:false,
                            dataType:'json',
                            url:hostUrl()+'login/checkPhone',
                            data:{phone:phone},
                            beforeSend:function(){

                            },
                            success:function(json){
                                if(!json.status){
                                    alertMessage(json.messages);
                                }else{
                                    voucher.setTime(obj);
                                }
                            }
                        });
                    }
                });
            },
            submit:function(){
                $('#voucher-form').on('click', '#voucher-submit', function(event){
                    var obj = $(this);
                    if (!obj.hasClass('btn-disabled')) {
                        var code = $.trim($('input[name=code]').val());
                        var phone = $.trim($('input[name=phone]').val());
                       
                        if (!validMobile(phone)) {
                            alertMessage('手机格式错误');
                            return false;
                        }
                        
                        $.ajax({
                            type:'post',
                            async:false,
                            dataType:'json',
                            url:hostUrl()+'voucher/getVoucher',
                            data:$('form').serialize(),
                            beforeSend:function(){

                            },
                            success:function(json){
                                if(json.status){
                                    window.location.href = hostUrl() + json.messages;
                                }else{
                                    alertMessage(json.messages);
                                }
                            }
                        });
                    }
                });
            },
            init:function(){
                voucher.submit();
                voucher.verify();
            }
        };

    if ($('form.login-form').size() > 0) {
        qlogin.init();
        nlogin.init();
    }

    if ($('.forget-form').size() > 0) {
        forget.init();
    }
    
    if ($('#register-from').size() > 0) {
        register.init();
    }

    if ($('#voucher-form').size() > 0) {
        voucher.init();
    }
    
});